package com.devicemantra.coordinate.interfaces.call;

import java.util.HashMap;
import java.util.Map;

import com.twilio.sdk.TwilioRestClient;
import com.twilio.sdk.TwilioRestException;
import com.twilio.sdk.TwilioRestResponse;

public class CMakeCall 
{
	/* Twilio REST API version */
    public static final String APIVERSION = "2008-08-01";
    /* Twilio AccountSid and AuthToken */
    private static String AccountSid = "AC77943b640133a1753309c10b540ab3fe";
    private static String  AuthToken = "162fb8a1140d3085fed1584d5250ee18";
    private static TwilioRestClient client = null;
    
    static
    {
    	client = new TwilioRestClient(AccountSid, AuthToken, null);
    }
    
    /* Outgoing Caller ID previously validated with Twilio */
    private static String CallerID = "8669296421";
    //private static String CallerID = "971559266628";


    public static void makeCallExample(String lCalled, String lCallUrl ){
    
  //build map of post parameters 
    Map<String,String> params = new HashMap<String,String>();
    params.put("Caller", CallerID);
    params.put("Called", lCalled);
    params.put("Url", lCallUrl);
    TwilioRestResponse response;
    try {
        response = client.request("/"+APIVERSION+"/Accounts/"+ client.getAccountSid()+"/Calls", "POST", params);
    
        if(response.isError())
            System.out.println("Error making outgoing call: "+response.getHttpStatus()+"\n"+response.getResponseText());
        else {
            System.out.println(response.getResponseText());

        }
    } catch (TwilioRestException e) {
        e.printStackTrace();
    }
    
    getCallsExample(client);
}

    private static void getCallsExample(TwilioRestClient client){
        TwilioRestResponse response;
        try {
            response = client.request("/"+APIVERSION+"/Accounts/"+client.getAccountSid()+"/Calls/CA93fb8bdfa0d1c28f0f1fa71eea62bb13", "GET",null);
        
            if(response.isError())
                System.out.println("Error fetching recent calls: "+response.getHttpStatus()+"\n"+response.getResponseText());
            else {
                System.out.println(response.getResponseText());
            }
        } catch (TwilioRestException e) {
            e.printStackTrace();
        }
    }
    
    
    

}
